package com.fr.data.impl.sap;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.fr.data.AbstractDataModel;
import com.fr.general.GeneralUtils;
import com.fr.general.data.TableDataException;
import com.fr.stable.StableUtils;
import com.fr.stable.StringUtils;

public class CustomFunctionSAPDataModel extends AbstractDataModel {
	private String connectionName;
	private String bapiName;
	private String choosedTable;
	private String[] columnNames;
	private Map inputs;
    private static final int JDK5 = 5;
	
	private List tableData = null;

	public CustomFunctionSAPDataModel(String connectionName, String bapiName,
			String choosedTable, String[] columnNames, Map inputs) {
		this.connectionName = connectionName;
		this.bapiName = bapiName;
		this.choosedTable = choosedTable;
		this.columnNames = columnNames;
		this.inputs = inputs;
	}

	public int getColumnCount() throws TableDataException {
		return columnNames == null ? 0 : columnNames.length;
	}

	public String getColumnName(int columnIndex) throws TableDataException {
		return columnNames == null ? null : columnNames[columnIndex];
	}

	public int getRowCount() throws TableDataException {
		initData();
		return tableData.size();
	}

	public Object getValueAt(int rowIndex, int columnIndex)
			throws TableDataException {
		initData();
		return columnIndex < 0 ? null : (hasRow(rowIndex) ? ((Object[])tableData.get(rowIndex))[columnIndex] : null);
	}
	
	private void initData() throws TableDataException {
		if (this.tableData == null) {
			this.tableData = new ArrayList();
			
			try {
				if (StringUtils.isNotBlank(this.choosedTable)) {
                    if(StableUtils.getMajorJavaVersion() < JDK5){
					SAPUtils.getBapiTableData4DifferentJDK(this.connectionName, this.bapiName, this.inputs, this.choosedTable, columnNames, null, tableData);
                    }else{
                        Class SAPUtilsJDK5Class;
                        SAPUtilsJDK5Class = GeneralUtils.classForName("com.fr.data.impl.sap.SAPUtilsJDK5");
                        Method method = SAPUtilsJDK5Class.getMethod("getBapiTableData4DifferentJDK", new Class[]{
                                String.class,
                                String.class,
                                Map.class,
                                String.class,
                                String[].class,
                                List.class,
                                List.class
                        });
                        method.invoke(SAPUtilsJDK5Class, new Object[]{
                                this.connectionName,
                                this.bapiName,
                                this.inputs,
                                this.choosedTable,
                                columnNames,
                                null,
                                tableData
                        });
                    }
                }
			} catch (Exception e) {
				throw new TableDataException(e.getMessage(), e);
			}
		}
	}

	public void release() throws Exception {
		if (this.tableData != null) {
			this.tableData.clear();
			this.tableData = null;
		}
	}

}
